﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Logistics
{
    public partial class frmChangePassword : Form
    {
        public frmChangePassword()
        {
            InitializeComponent();
        }

        private void butSave_Click(object sender, EventArgs e)
        {
            if (!VerifyFrom())
                return;
            var msg = "确定要修改密码吗？";
            if (MessageBox.Show(msg, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                var objUser = new Manager.tc_User();
                MyForms.User.Password = txtNewPassword.Text.Trim();
                objUser.Update(MyForms.User);
                MessageBox.Show("数据保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }
        private bool VerifyFrom()
        {
            var msg = string.Empty;
            if (string.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                msg = "原密码不能为空";
                MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPassword.Focus();
                return false;
            }
            if (MyForms.User.Password != txtPassword.Text.Trim())
            {
                msg = "原密码不正确";
                MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPassword.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtNewPassword.Text.Trim()))
            {
                msg = "新密码不能为空";
                MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtNewPassword.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtCfmPassword.Text.Trim()))
            {
                msg = "确认密码不能为空";
                MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtCfmPassword.Focus();
                return false;
            }
            if (txtNewPassword.Text.Trim() != txtCfmPassword.Text.Trim())
            {
                msg = "确认密码不能正确";
                MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtCfmPassword.Focus();
                return false;
            }
            return true;
        }

        private void butCancel_Click(object sender, EventArgs e)
        {
            var msg = "数据尚未保存，确认要取消吗？";
            if (MessageBox.Show(msg, "警告", MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void frmChangePassword_Load(object sender, EventArgs e)
        {
            DataBind();
        }
        private void DataBind()
        {
            txtUserName.Text = MyForms.User.UserName;
        }
    }
}
